package com.sky.controller.admin;

import com.sky.po.CompleteEx;
import com.sky.po.FenhangEx;
import com.sky.result.Result;
import com.sky.service.HandleService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.regex.Pattern;

@RestController
//@RequestMapping("/handle")
@Api(tags = "通用接口")
@Slf4j
public class HandleController {

    @Autowired
    private HandleService handleService;

    /**
     * 根据前端传进来的数据按分号分行
     *
     * @param fenhangEx
     * @return
     * @throws Exception
     */
    @PostMapping("/handle/fenhang")
    public Result<List<CompleteEx>> upload(@RequestBody FenhangEx fenhangEx) {
        String qnameList = fenhangEx.getQnameList();
        String[] qnameArray = Pattern.compile("[；;]").split(qnameList);
        // 将数组转化为列表
        List<String> qnameListArray = Arrays.asList(qnameArray);
        List<CompleteEx> completeExList = new ArrayList<>();
        for (String qname : qnameListArray) {
            String lastTwoChars = qname.substring(qname.length() - 2);
            String lastFChars = qname.substring(qname.length() - 4);
            if (lastTwoChars.equals("壹级") || lastTwoChars.equals("贰级") ||
                    lastTwoChars.equals("叁级")|| lastTwoChars.equals("肆级")||
                    lastTwoChars.equals("特级")||lastTwoChars.equals("甲级")||
                    lastTwoChars.equals("乙级")||lastTwoChars.equals("丙级") ||
                    lastTwoChars.equals("一级") || lastTwoChars.equals("二级") ||
                    lastTwoChars.equals("三级")|| lastTwoChars.equals("四级")) {
                // 继续处理
                String otherChars = qname.substring(0, qname.length() - 2);
                CompleteEx completeEx = new CompleteEx();
                completeEx.setQname(otherChars);
                completeEx.setQposition(lastTwoChars);
                completeEx.setNumber(fenhangEx.getNumber());
                completeEx.setDate(fenhangEx.getDate());
                completeEx.setFname(fenhangEx.getFname());
                completeEx.setFtype(fenhangEx.getFtype());
                completeEx.setAdress(fenhangEx.getAdress());
                completeEx.setPrice(fenhangEx.getPrice());
                completeEx.setLperson(fenhangEx.getLperson());
                completeEx.setCperson(fenhangEx.getCperson());
                completeEx.setCposition(fenhangEx.getCposition());
                completeEx.setCphone(fenhangEx.getCphone());
                completeEx.setRperson(fenhangEx.getRperson());
                completeEx.setRphone(fenhangEx.getRphone());
                if (otherChars.contains("新能源")) {
                    completeEx.setStatus(2);
                } else if (otherChars.contains("环保")) {
                    completeEx.setStatus(3);
                } else if (otherChars.contains("建筑")) {
                    completeEx.setStatus(4);
                } else if (otherChars.contains("钢")) {
                    completeEx.setStatus(5);
                } else if (otherChars.contains("园林")) {
                    completeEx.setStatus(6);
                } else if (otherChars.contains("水")) {
                    completeEx.setStatus(7);
                } else if (otherChars.contains("市政")) {
                    completeEx.setStatus(8);
                } else if (otherChars.contains("电力")) {
                    completeEx.setStatus(9);
                } else if (otherChars.contains("机电")) {
                    completeEx.setStatus(10);
                } else if (otherChars.contains("消防")) {
                    completeEx.setStatus(11);
                } else if (otherChars.contains("铁路")) {
                    completeEx.setStatus(12);
                } else if (otherChars.contains("公路")) {
                    completeEx.setStatus(13);
                } else if (otherChars.contains("桥梁工程")) {
                    completeEx.setStatus(14);
                } else if (otherChars.contains("隧道")) {
                    completeEx.setStatus(15);
                } else if (otherChars.contains("矿山")) {
                    completeEx.setStatus(16);
                } else if (otherChars.contains("桥梁")) {
                    completeEx.setStatus(17);
                } else if (otherChars.contains("特种") || otherChars.contains("压力") || otherChars.contains("模板")) {
                    completeEx.setStatus(18);
                } else if (otherChars.contains("机场") || otherChars.contains("港口") || otherChars.contains("航道")) {
                    completeEx.setStatus(19);
                } else if (otherChars.contains("石油化工")) {
                    completeEx.setStatus(20);
                } else if (otherChars.contains("装修") || otherChars.contains("装饰") || otherChars.contains("幕墙")) {
                    completeEx.setStatus(21);
                } else if (otherChars.contains("防水防腐")) {
                    completeEx.setStatus(22);
                } else if (otherChars.contains("维护")) {
                    completeEx.setStatus(23);
                } else if (otherChars.contains("冶金")) {
                    completeEx.setStatus(24);
                } else if (otherChars.contains("施工劳务")) {
                    completeEx.setStatus(25);
                } else if (otherChars.contains("测绘") || otherChars.contains("地质") || otherChars.contains("勘察") || otherChars.contains("修复")) {
                    completeEx.setStatus(26);
                } else if (otherChars.contains("拆除") || otherChars.contains("爆破")) {
                    completeEx.setStatus(27);
                } else if (otherChars.contains("电子") || otherChars.contains("智能化")) {
                    completeEx.setStatus(28);
                } else if (otherChars.contains("河湖")) {
                    completeEx.setStatus(29);
                } else if (otherChars.contains("混凝土")) {
                    completeEx.setStatus(30);
                } else if (otherChars.contains("起重")) {
                    completeEx.setStatus(31);
                } else if (otherChars.contains("通信")) {
                    completeEx.setStatus(32);
                } else if (otherChars.contains("设计")) {
                    completeEx.setStatus(33);
                } else if (otherChars.contains("电缆")|| otherChars.contains("电线")) {
                    completeEx.setStatus(34);
                } else if (otherChars.contains("照明")) {
                    completeEx.setStatus(35);
                } else if (otherChars.contains("保安")) {
                    completeEx.setStatus(36);
                } else if (otherChars.contains("环卫")) {
                    completeEx.setStatus(37);
                }else if (otherChars.contains("监理")) {
                    completeEx.setStatus(38);
                }else if (otherChars.contains("造价")) {
                    completeEx.setStatus(39);
                }else if (otherChars.contains("审计")) {
                    completeEx.setStatus(40);
                }else if (otherChars.contains("招标代理")) {
                    completeEx.setStatus(41);
                }else if (otherChars.contains("采购代理")) {
                    completeEx.setStatus(42);
                }else if (otherChars.contains("物业")) {
                    completeEx.setStatus(43);
                }else if (otherChars.contains("网络通讯")) {
                    completeEx.setStatus(45);
                }else if (otherChars.contains("工程咨询")) {
                    completeEx.setStatus(46);
                }else if (otherChars.contains("安全评估")) {
                    completeEx.setStatus(47);
                }else if (otherChars.contains("家政")) {
                    completeEx.setStatus(48);
                }else if (otherChars.contains("道路运输")) {
                    completeEx.setStatus(49);
                }else if (otherChars.contains("餐饮")|| otherChars.contains("食品")) {
                    completeEx.setStatus(50);
                }else if (otherChars.contains("土石方")) {
                    completeEx.setStatus(51);
                }else if (otherChars.contains("设备")) {
                    completeEx.setStatus(52);
                }else if (otherChars.contains("岩土工程")) {
                    completeEx.setStatus(53);
                }else if (otherChars.contains("工程测量")) {
                    completeEx.setStatus(54);
                }else if (otherChars.contains("水文地质")) {
                    completeEx.setStatus(55);
                }else if (otherChars.contains("生态环境环境工程")) {
                    completeEx.setStatus(56);
                }else if (otherChars.contains("土木工程")) {
                    completeEx.setStatus(57);
                }
                else {
                    completeEx.setStatus(44);
                }
                completeExList.add(completeEx);
            }
             else if(lastFChars.equals("不分等级")){
                // 其他操作
                String otherChars = qname.substring(0, qname.length() - 4);
                CompleteEx completeEx = new CompleteEx();
                completeEx.setQname(otherChars);
                completeEx.setQposition(lastFChars);
                completeEx.setNumber(fenhangEx.getNumber());
                completeEx.setDate(fenhangEx.getDate());
                completeEx.setFname(fenhangEx.getFname());
                completeEx.setFtype(fenhangEx.getFtype());
                completeEx.setAdress(fenhangEx.getAdress());
                completeEx.setPrice(fenhangEx.getPrice());
                completeEx.setLperson(fenhangEx.getLperson());
                completeEx.setCperson(fenhangEx.getCperson());
                completeEx.setCposition(fenhangEx.getCposition());
                completeEx.setCphone(fenhangEx.getCphone());
                completeEx.setRperson(fenhangEx.getRperson());
                completeEx.setRphone(fenhangEx.getRphone());
                if (otherChars.contains("新能源")) {
                    completeEx.setStatus(2);
                } else if (otherChars.contains("环保")) {
                    completeEx.setStatus(3);
                } else if (otherChars.contains("建筑")) {
                    completeEx.setStatus(4);
                } else if (otherChars.contains("钢")) {
                    completeEx.setStatus(5);
                } else if (otherChars.contains("园林")) {
                    completeEx.setStatus(6);
                } else if (otherChars.contains("水")) {
                    completeEx.setStatus(7);
                } else if (otherChars.contains("市政")) {
                    completeEx.setStatus(8);
                } else if (otherChars.contains("电力")) {
                    completeEx.setStatus(9);
                } else if (otherChars.contains("机电")) {
                    completeEx.setStatus(10);
                } else if (otherChars.contains("消防")) {
                    completeEx.setStatus(11);
                } else if (otherChars.contains("铁路")) {
                    completeEx.setStatus(12);
                } else if (otherChars.contains("公路")) {
                    completeEx.setStatus(13);
                } else if (otherChars.contains("桥梁工程")) {
                    completeEx.setStatus(14);
                } else if (otherChars.contains("隧道")) {
                    completeEx.setStatus(15);
                } else if (otherChars.contains("矿山")) {
                    completeEx.setStatus(16);
                } else if (otherChars.contains("桥梁")) {
                    completeEx.setStatus(17);
                } else if (otherChars.contains("特种") || otherChars.contains("压力") || otherChars.contains("模板")) {
                    completeEx.setStatus(18);
                } else if (otherChars.contains("机场") || otherChars.contains("港口") || otherChars.contains("航道")) {
                    completeEx.setStatus(19);
                } else if (otherChars.contains("石油化工")) {
                    completeEx.setStatus(20);
                } else if (otherChars.contains("装修") || otherChars.contains("装饰") || otherChars.contains("幕墙")) {
                    completeEx.setStatus(21);
                } else if (otherChars.contains("防水防腐")) {
                    completeEx.setStatus(22);
                } else if (otherChars.contains("维护")) {
                    completeEx.setStatus(23);
                } else if (otherChars.contains("冶金")) {
                    completeEx.setStatus(24);
                } else if (otherChars.contains("施工劳务")) {
                    completeEx.setStatus(25);
                } else if (otherChars.contains("测绘") || otherChars.contains("地质") || otherChars.contains("勘察") || otherChars.contains("修复")) {
                    completeEx.setStatus(26);
                } else if (otherChars.contains("拆除") || otherChars.contains("爆破")) {
                    completeEx.setStatus(27);
                } else if (otherChars.contains("电子") || otherChars.contains("智能化")) {
                    completeEx.setStatus(28);
                } else if (otherChars.contains("河湖")) {
                    completeEx.setStatus(29);
                } else if (otherChars.contains("混凝土")) {
                    completeEx.setStatus(30);
                } else if (otherChars.contains("起重")) {
                    completeEx.setStatus(31);
                } else if (otherChars.contains("通信")) {
                    completeEx.setStatus(32);
                } else if (otherChars.contains("设计")) {
                    completeEx.setStatus(33);
                } else if (otherChars.contains("电缆")|| otherChars.contains("电线")) {
                    completeEx.setStatus(34);
                } else if (otherChars.contains("照明")) {
                    completeEx.setStatus(35);
                } else if (otherChars.contains("保安")) {
                    completeEx.setStatus(36);
                } else if (otherChars.contains("环卫")) {
                    completeEx.setStatus(37);
                }else if (otherChars.contains("监理")) {
                    completeEx.setStatus(38);
                }else if (otherChars.contains("造价")) {
                    completeEx.setStatus(39);
                }else if (otherChars.contains("审计")) {
                    completeEx.setStatus(40);
                }else if (otherChars.contains("招标代理")) {
                    completeEx.setStatus(41);
                }else if (otherChars.contains("采购代理")) {
                    completeEx.setStatus(42);
                }else if (otherChars.contains("物业")) {
                    completeEx.setStatus(43);
                }else if (otherChars.contains("网络通讯")) {
                    completeEx.setStatus(45);
                }else if (otherChars.contains("工程咨询")) {
                    completeEx.setStatus(46);
                }else if (otherChars.contains("安全评估")) {
                    completeEx.setStatus(47);
                }else if (otherChars.contains("家政")) {
                    completeEx.setStatus(48);
                }else if (otherChars.contains("道路运输")) {
                    completeEx.setStatus(49);
                } else if (otherChars.contains("餐饮")|| otherChars.contains("食品")) {
                    completeEx.setStatus(50);
                }else if (otherChars.contains("土石方")) {
                    completeEx.setStatus(51);
                }else if (otherChars.contains("设备")) {
                    completeEx.setStatus(52);
                }else if (otherChars.contains("岩土工程")) {
                    completeEx.setStatus(53);
                }else if (otherChars.contains("工程测量")) {
                    completeEx.setStatus(54);
                }else if (otherChars.contains("水文地质")) {
                    completeEx.setStatus(55);
                }else if (otherChars.contains("生态环境环境工程")) {
                    completeEx.setStatus(56);
                }else if (otherChars.contains("土木工程")) {
                    completeEx.setStatus(57);
                }
                else {
                    completeEx.setStatus(44);
                }
                completeExList.add(completeEx);
            }
            }
        return Result.success(completeExList);
    }

    @PostMapping("/handle/submit")
    public Result<List<CompleteEx>> submit(@RequestBody List<CompleteEx> exList){
        System.out.println(exList);
        for (CompleteEx ex : exList) {
            handleService.addOneRecord(ex);
        }
        return Result.success();
    }
}